package com;

import java.util.ArrayList;

import database.databaseInteraction;
import database.database_auth;
import com.database.databaseElement;

public class discussion extends databaseElement {

	public String fromUserID = "0";
	public String discussionParentID = "0"; // if this is a sub-discussion, then give me the ID of it's parent-discussion (DISCUSSIONID)
	public String name = ""; // unique discussion name (max 300 chars)
	public String description = "";
	public String submitted = "0";
	public String updated = "0";
	public String deleted = "0";
	public String lastChange = "0";
	public String rank = "0";
	public String importance = "0";
	public String pollIDs = "0";
	public String priority = "0";
	public String MessageCount = "0";
	public String viewed = "0";

	public static databaseInteraction DatabaseConnection = new databaseInteraction();
	public static database_auth database_auth_inst = new database_auth(); // will be read from configuration file endpoint_xml.config -> host,port,DatabaseName,type,TableName,user,password

	/** constructor message 
	 * **/
	public discussion(database_auth database_auth_inst, message MESSAGE_inst)
	{
		super(database_auth_inst, "discussions", "DISCUSSIONID");
		
		this.database_auth_inst = database_auth_inst;
		this.fromUserID = MESSAGE_inst.fromUserID;
		if(MESSAGE_inst.DISCUSSION != null)
		{
			this.name = MESSAGE_inst.DISCUSSION.name;
			this.description = MESSAGE_inst.DISCUSSION.description;
		}
		this.submitted = MESSAGE_inst.timeSend;
	}

	/** constructor chat session 
	 * **/
	public discussion(database_auth database_auth_inst, chatSession CHATSESSION_inst)
	{
		super(database_auth_inst, "discussions", "DISCUSSIONID");
		
		this.database_auth_inst = database_auth_inst;
		this.fromUserID = CHATSESSION_inst.getfromUserID();
		if(CHATSESSION_inst.DISCUSSION != null)
		{
			this.name = CHATSESSION_inst.DISCUSSION.name;
			this.description = CHATSESSION_inst.DISCUSSION.description;
		}
		this.submitted = CHATSESSION_inst.TimeLastMessage;
	}
	
	/** alternative constructor without the message 
	 * **/
	public discussion(database_auth database_auth_inst)
	{
		super(database_auth_inst, "discussions", "DISCUSSIONID");
		
		this.database_auth_inst = database_auth_inst;
	}

	// status of last database operation
	String status = "";

	/** creates a new discussion entry in table discussions (existing entries will not be overwritten)
	 * 
	 * if entry allready exists returns false and status "discussion exists allready"
	 * **/
	public Boolean createDiscussion()
	{
		// generate new DISCUSSIONID
		this.ID = generateNewID(database_auth_inst, this.tableName, this.IDKEY);

		String question = "INSERT INTO `together`.`discussions` "+
		       "(`ID`,   `DISCUSSIONID`,     `fromUserID`	,    `name`  ,   `discussionParentID`,     `description`  ,   `submitted`  ,   `updated`,     `deleted`,      `lastChange`,    `rank`,     `importance`,     `pollIDs`,      `priority`,     `MessageCount`,     `viewed`) "+
		"VALUES (NULL, '"+ID+"',           '"+fromUserID+"', '"+name+"', '"+discussionParentID+"','"+description+"', '"+submitted+"', '"+updated+"', '"+deleted+"', '"+lastChange+"', '"+rank+"', '"+importance+"', '"+pollIDs+"', '"+priority+"', '"+MessageCount+"', '"+viewed+"');";
		
		DatabaseConnection.askDatabase(database_auth_inst, question); // StringBuffer answer = DatabaseConnection.askDatabase( host, DatabaseName, TableName, user, password, question);
		status = DatabaseConnection.status;
		return DatabaseConnection.success;
	}

	/** get all IDs of all discussions that user has subscribed to **/
	public ArrayList<Object> UserDiscussionSubscriptions(String USERID, database_auth database_auth_inst)
	{
		String question = "SELECT * FROM `discussionSubscriptions` WHERE `USERID` = "+USERID+";"; // mysql function to generate passsword hash: SELECT PASSWORD('djfkldjfkld!');
		
		DatabaseConnection.askDatabase(database_auth_inst, question); // StringBuffer answer = DatabaseConnection.askDatabase( host, DatabaseName, TableName, user, password, question);
		
		ArrayList<Object> discussionSubscriptions = (ArrayList<Object>) DatabaseConnection.answerJAVA.clone();
		
		return discussionSubscriptions;
	}
}